package cn.xiongxf.basic.service.impl;

import cn.xiongxf.basic.mapper.BaseMapper;
import cn.xiongxf.basic.query.BaseQuery;
import cn.xiongxf.basic.service.BaseService;
import cn.xiongxf.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
/**
 * 业务实现类
 */
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS) // 含@Inherited，能被继承
public class BaseServiceImpl<T> implements BaseService<T> {
    @Autowired
    private BaseMapper<T> baseMapper;   // 加泛型，再删错误包

    @Override
    public List<T> findAll() {
        return baseMapper.loadAll();
    }

    @Override
    public T findById(Long id) {
        return baseMapper.loadById(id);
    }

    @Transactional
    @Override
    public void del(Long id) {
        baseMapper.remove(id);
    }

    @Transactional
    @Override
    public void add(T t) {
        baseMapper.save(t);
    }

    @Transactional
    @Override
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    public PageList<T> queryPageList(BaseQuery baseQuery) {
        // 先查询总条数
        Integer count = baseMapper.queryCount(baseQuery);
        if (count == null || count == 0){
            return new PageList<T>(); // 避免报空指针异常
        }
        // 再查询当前页的分页数据
       List<T> ts = baseMapper.queryData(baseQuery);
        return new PageList<T>(count,ts);
    }

    @Transactional
    @Override
    public void patchDel(List<Long> ids) {
        baseMapper.patchRemove(ids);
    }

}
